import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/stores/user';
import { useRouterListStore } from '@/stores/router'
// layout布局组件是所有二级路由的父亲，无论什么时候都需要加载出来的，所以不选用路由懒加载模式
import Layout from '@/Layout/index.vue';
import NotFound404 from '@/views/404.vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
    },
    {
      path: '/login',
      meta: {
        title: '登录'
      },
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '/dashboard',
          component: () => import('@/views/dashboard.vue'),
          meta: {
            title: '工作台'
          },
        },
        {
          path: '/manage-base-info/warehouse',
          component: () => import('@/views/ManageBaseInfo/warehouse.vue'),
          meta: {
            title: '仓库管理'
          },
        },
        {
          path: '/manage-base-info/area',
          component: () => import('@/views/ManageBaseInfo/area.vue'),
          meta: {
            title: '库区管理'
          },
        },
        {
          path: '/manage-base-info/location',
          component: () => import('@/views/ManageBaseInfo/location.vue'),
          meta: {
            title: '库位管理'
          },
        },
        {
          path: '/manage-base-info/view',
          component: () => import('@/views/ManageBaseInfo/location-view.vue'),
          meta: {
            title: '库位视图'
          },
        },
        {
          path: '/manage-base-info/goods',
          component: () => import('@/views/ManageBaseInfo/goods.vue'),
          meta: {
            title: '货品管理'
          },
        },
        {
          path: '/manage-base-info/goods-type',
          component: () => import('@/views/ManageBaseInfo/goods-type.vue'),
          meta: {
            title: '货品类别管理'
          },
        },
        {
          path: '/manage-business/goods-owner',
          component: () => import('@/views/ManageBusiness/goods-owner.vue'),
          meta: {
            title: '货主管理'
          },
        },
        {
          path: '/manage-business/task-picking',
          component: () => import('@/views/ManageBusiness/task-picking.vue'),
          meta: {
            title: '承运商管理'
          },
        },
        {
          path: '/manage-storage/list',
          component: () => import('@/views/ManageStorage/list.vue'),
          meta: {
            title: '盘点单'
          },
        },
        {
          path: '/manage-storage/list-task',
          component: () => import('@/views/ManageStorage/list-task.vue'),
          meta: {
            title: '盘点任务'
          },
        },
        {
          path: '/manage-storage/storage-lose-profit',
          component: () => import('@/views/ManageStorage/storage-lose-profit.vue'),
          meta: {
            title: '库存损益'
          },
        },
        {
          path: '/manage-storage/latest-storage',
          component: () => import('@/views/ManageStorage/latest-storage.vue'),
          meta: {
            title: '实时库存'
          },
        },
        {
          path: '/manage-storage-in/list-in/list',
          component: () => import('@/views/ManageStorageIn/list.vue'),
          meta: {
            title: '入库单'
          },
        },
        {
          path: '/manage-storage-in/list-in/task-receive',
          component: () => import('@/views/ManageStorageIn/task-receive.vue'),
          meta: {
            title: '收货任务'
          },
        },
        {
          path: '/manage-storage-in/list-in/task-add',
          component: () => import('@/views/ManageStorageIn/task-add.vue'),
          meta: {
            title: '上架任务'
          },
        },
        {
          path: '/manage-storage-out/list-out',
          component: () => import('@/views/ManageStorageOut/list-out.vue'),
          meta: {
            title: '出库单'
          },
        },
        {
          path: '/manage-storage-out/task-picking',
          component: () => import('@/views/ManageStorageOut/task-picking.vue'),
          meta: {
            title: '拣货任务'
          },
        },
        {
          path: '/manage-storage-out/manage-task-transfer',
          component: () => import('@/views/ManageStorageOut/manage-task-transfer.vue'),
          meta: {
            title: '交接任务'
          },
        },
      ]
    },
    {
      // path: '*',  路由3.x版本的， 我们应该用4.x版本的  正则匹配
      path: '/:pathMatch(.*)*',
      component: NotFound404,
    },
  ]
})

router.beforeEach((to) => {
  const store = useUserStore();
  const useRouterList = useRouterListStore();
  // 合理的使用reutrn 代替else 语句，精简代码
  if (!store.token) {
    if (!useRouterList.getRouterList().find(item => item.path === to.path) && to.path !== '/login') {
      useRouterList.setRouterList(to);
    }
    // 如果登录有，去任何页面直接放行
    return true; // 等价于之前vue-router 3.x的 next()
  }
  if (to.path === '/login') {
    return true; // next()
  }
  return '/login'; //next('/login')
});

export default router
